home *** CD-ROM | disk | FTP | other *** search
/ T&A 2 the Maxx 3 / T and A 2 The Maxx Number 3.iso / viewers / unixview / xgiftar.z / xgiftar / smooth.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  2KB  |  95 lines

  1. /* smooth.c:
  2.  *
  3.  * this performs a smoothing convolution using a 3x3 area.
  4.  *
  5.  * jim frost 09.20.90
  6.  *
  7.  * Copyright 1990, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. static Image *doSmooth(image)
  15.      Image *image;
  16. { Image *old, *new;
  17.   int    x, y, x1, y1, linelen;
  18.   int    xindex[3];
  19.   byte  *yindex[3];
  20.   byte  *destptr;
  21.   Pixel  pixval;
  22.   unsigned long avgred, avggreen, avgblue;
  23.  
  24.   /* build true color image from old image and allocate new image
  25.    */
  26.  
  27.   old= expand(image);
  28.   new= newTrueImage(image->width, image->height);
  29.   new->title= (char *)lmalloc(strlen(image->title) + 12);
  30.   sprintf(new->title, "%s (smoothed)", image->title);
  31.  
  32.   /* run through image and take a guess as to what the color should
  33.    * actually be.
  34.    */
  35.  
  36.   destptr= new->data;
  37.   linelen= old->pixlen * old->width;
  38.   for (y= 0; y < old->height; y++) {
  39.     yindex[1]= old->data + (y * linelen);
  40.     yindex[0]= yindex[1] - (y > 0 ? linelen : 0);
  41.     yindex[2]= yindex[1] + (y < old->height - 1 ? linelen : 0);
  42.     for (x= 0; x < old->width; x++) {
  43.       avgred= avggreen= avgblue= 0;
  44.       xindex[1]= x * old->pixlen;
  45.       xindex[0]= xindex[1] - (x > 0 ? old->pixlen : 0);
  46.       xindex[2]= xindex[1] + (x < old->width - 1 ? old->pixlen : 0);
  47.       for (y1= 0; y1 < 3; y1++) {
  48.     for (x1= 0; x1 < 3; x1++) {
  49.       pixval= memToVal(yindex[y1] + xindex[x1], old->pixlen);
  50.       avgred += TRUE_RED(pixval);
  51.       avggreen += TRUE_GREEN(pixval);
  52.       avgblue += TRUE_BLUE(pixval);
  53.     }
  54.       }
  55.  
  56.       /* average the pixel values
  57.        */
  58.  
  59.       avgred= ((avgred + 8) / 9);
  60.       avggreen= ((avggreen + 8) / 9);
  61.       avgblue= ((avgblue + 8) / 9);
  62.       pixval= (avgred << 16) | (avggreen << 8) | avgblue;
  63.       valToMem(pixval, destptr, new->pixlen);
  64.       destptr += new->pixlen;
  65.     }
  66.   }
  67.  
  68.   if (old != image)
  69.     freeImage(old);
  70.   return(new);
  71. }
  72.  
  73. Image *smooth(image, iterations, verbose)
  74.      Image *image;
  75.      int    verbose;
  76. { int a;
  77.   Image *new;
  78.  
  79.   if (verbose) {
  80.     printf("  Smoothing...");
  81.     fflush(stdout);
  82.   }
  83.  
  84.   for (a= 0; a < iterations; a++) {
  85.     new= doSmooth(image);
  86.     freeImage(image);
  87.     image= new;
  88.   }
  89.  
  90.   if (verbose)
  91.     printf("done\n");
  92.  
  93.   return(image);
  94. }
  95.